const mongoose=require('mongoose');
const Comments = require('../models/comments.model');
const News     = require('../models/news.model')

exports.create=function(req,res,next){
    const kitty = new Comments(req.body);
    kitty.save().then(data => {
        res.json(data);
    });
}

exports.updata=function(req,res,next){
    var id = req.params.id;
    Comments.findByIdAndUpdate(id,{$set:req.body},{new:false})
    .then(data=>{
        res.json(data);
    })
}

exports.get=function(req,res,next){
    var id = req.params.id;
    Comments.findById(id)
    .then(data=>{
        res.json(data);
    })
}

exports.remove=function(req,res,next){
    var id = req.params.id;
    Comments.findByIdAndRemove(id)
    .then(data=>{
        res.json(data);
    })
}

exports.removes=function(req,res,next){
    // var ids = req.body.ids.split(',');
    // Users.remove({_id:{ $in: ids}}).then(()=>{
    //     res.json({"message":"删除记录成功",status:200})
    // })
}

exports.list = function (req, res, next) {
    var page           = req.body.page ? req.body.page : 1;
    var rows           = req.body.rows ? req.body.rows : 5;
    var queryCondition = {newId:req.body.newId}

    Comments.paginate(queryCondition, { page: +page, limit: +rows }, function (err, result) {
        result.rows = result.docs
        delete result.docs
        if(result.rows.length==0){
            result.page = result.page-1
        }
        res.json(result)
    })
}